package name.sraynal.msgdispatcher.controller;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

import name.sraynal.msgdispatcher.event.Event;
import name.sraynal.msgdispatcher.model.DispatcherModel;
import name.sraynal.msgdispatcher.view.EndView;
import name.sraynal.msgdispatcher.view.FilesView;
import name.sraynal.msgdispatcher.view.TextView;

public class ApplicationController extends Controller {
	
	public void setMidlet(MIDlet midlet) {
		this.midlet = midlet;
	}

	private MIDlet midlet;
	private FileController fileController;
	private TextController textController;
	private TextView textView;
	private FilesView filesView;
	private EndView endView;
	
	public ApplicationController(DispatcherModel model, Display display) {
		super(model, display);
		Controller[] controllers;
		
		this.fileController = new FileController(this.model, this.display);
		this.textController = new TextController(this.model, this.display);
		
		
		controllers = new Controller[] {this.textController};
		this.textView = new TextView(controllers);
		this.textController.setTextView(this.textView);
		
		controllers = new Controller[] {this.textController, this.fileController};
		this.filesView = new FilesView(controllers);
		this.fileController.setFilesView(this.filesView);
		this.textController.setFilesView(this.filesView);
		
		controllers = new Controller[] {this, this.fileController};
		this.endView = new EndView(controllers);
		this.fileController.setEndView(this.endView);
		
	}

	public void sendEvent(Event event) {
		if (event.getType() == Event.START_APP) {
			displayView(this.textView);
		} else if (event.getType() == Event.END_APP) {
			stop();
		} else if (event.getType() == Event.BACK_TO_BEGINNING) {
			displayView(this.textView);
		}
	}
	
    public void stop() {
        this.display.setCurrent(null);
        this.midlet.notifyDestroyed();
    }
}
